<template>
  <div class="table-main">
    <el-dialog
      :close-on-click-modal="false"
      @close="handMyClose"
      :append-to-body="true"
      title="优惠券"
      :visible.sync="dialogVisible1"
      width="60%"
    >
      <div class="form_box">
        <el-form ref="form" :model="form" label-width="120px" :rules="rules">
          <el-form-item label="优惠券名称:" prop="couponName">
            <el-row :gutter="20">
              <el-col :span="21">
                <el-input v-model="form.couponName" placeholder="请输入优惠券名称" :disabled="isShow==3"></el-input>
              </el-col>
            </el-row>
          </el-form-item>
          <el-form-item label="优惠金额:" prop="reduceMoeny">
            <el-row :gutter="20">
              <el-col :span="21">
                <el-input v-model="form.reduceMoeny" placeholder="请输入" :disabled="isShow==3"></el-input>
              </el-col>
            </el-row>
          </el-form-item>
          <el-form-item label="最低消费可用:" prop="fullMoeny">
            <el-row :gutter="20">
              <el-col :span="21">
                <el-input v-model="form.fullMoeny" placeholder="请输入" :disabled="isShow==3"></el-input>
              </el-col>
            </el-row>
          </el-form-item>
          <el-form-item label="发放总数:" prop="num">
            <el-row :gutter="20">
              <el-col :span="21">
                <el-input v-model="form.num" placeholder="请输入" :disabled="isShow==3"></el-input>
              </el-col>
            </el-row>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible1 = false">取 消</el-button>
        <el-button
          :loading="bntLoading"
          v-if="isShow!=3"
          type="primary"
          @click="handFromSub('form')"
        >确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import BaiduMap from 'vue-baidu-map/components/map/Map.vue'
import BmView from 'vue-baidu-map/components/map/MapView.vue'
import BmLocalSearch from 'vue-baidu-map/components/search/LocalSearch.vue'
import { imgSizi } from '@/utils/myUtils'
export default {
  name: 'SidebarLogo',
  components: {
    BaiduMap,
    BmView,
    BmLocalSearch
  },
  watch: {
    isState(newObj) {
      this.isShow = newObj
    }
  },
  props: {
    isState: {
      type: Number,
      default: 0
    },
    newId: {
      type: Number
    }
  },
  data() {
    return {
      bntLoading: false,
      headers: {
        token: localStorage.getItem('mytoken') || ''
      },
      rules: {
        couponName: [
          { required: true, message: '请输入店铺全程', trigger: 'blur' }
        ],
        endTime: [
          { required: true, message: '请选择过期时间', trigger: 'change' }
        ],
        reduceMoeny: [
          {
            required: true,
            message: '请输入优惠金额(只能输入数字)',
            trigger: 'blur'
          },
          {
            validator(rule, value, callback) {
              var reg = /^[0-9]{1}([0-9]|[.])*$/
              if (reg.test(value)) {
                callback()
              } else {
                callback(new Error('请输入优惠金额(只能输入数字)'))
              }
            },
            trigger: 'blur'
          }
        ],
        fullMoeny: [
          {
            required: true,
            message: '请输入最低消费金额(只能输入数字)',
            trigger: 'blur'
          },
          {
            validator(rule, value, callback) {
              var reg = /^[0-9]{1}([0-9]|[.])*$/
              if (reg.test(value)) {
                callback()
              } else {
                callback(new Error('请输入最低消费金额(只能输入数字)'))
              }
            },
            trigger: 'blur'
          }
        ],
        num: [
          {
            required: true,
            message: '请输入发放总数(只能输入数字)',
            trigger: 'blur'
          },
          {
            validator(rule, value, callback) {
              var reg = /^[0-9]{1}([0-9]|[])*$/
              if (reg.test(value)) {
                callback()
              } else {
                callback(new Error('请输入发放总数(只能输入数字)'))
              }
            },
            trigger: 'blur'
          }
        ]
      },
      form: {},
      isShow: 0,
      imageUrl: '',
      dialogImageUrl: '',
      dialogVisible2: false,
      dialogVisible1: false,
      shopStatusOptions: [
        {
          label: '营业中',
          value: 1
        },
        {
          label: '休息',
          value: 2
        },
        {
          label: '冻结',
          value: 3
        }
      ],
      location: {
        lng: 116.404,
        lat: 39.915
      },
      zoom: 12.8,
      addressKeyword: '',
      activeName: 'first',
      value4: [new Date(2020, 9, 10, 0, 0), new Date(2020, 9, 10, 24, 0)],
      dialogVisible: false
    }
  },
  mounted() {},
  methods: {
    handMyClose() {
      this.$refs.form.resetFields()
      this.$parent.closeFrom()
    },
    handGetShop(id) {
      this.$api.selectCouponByIdCoupon({ id }).then(res => {
        console.log('获取详情', res.data, id)
        this.form = res.data.data
      })
    },
    handFromSub(formName) {
      //1编辑
      if (this.isState == 1) {
        this.$refs[formName].validate(valid => {
          if (valid) {
            this.bntLoading = true
            this.$api
              .editCoupon(this.form)
              .then(res => {
                this.dialogVisible1 = false
                this.bntLoading = false
                this.$parent.init()
                console.log(res, '编辑', this.form)
                this.$message({
                  message: res.data.message,
                  type: 'success'
                })
              })
              .finally(() => {
                this.bntLoading = false
              })
          } else {
            console.log('error submit!!')
            this.$message({
              message: '请完成表单录入'
            })
            return false
          }
        })
      } else if (this.isState == 0) {
        this.$refs[formName].validate(valid => {
          if (valid) {
            this.bntLoading = true
            this.$api
              .addCoupon(this.form)
              .then(res => {
                console.log(res, '添加优惠券', this.form)
                this.bntLoading = false
                this.dialogVisible1 = false
                this.$parent.init()
                this.$refs.form.resetFields()
              })
              .finally(() => {
                this.bntLoading = false
              })
          } else {
            console.log('error submit!!')
            this.$message({
              message: '请完成表单录入'
            })
            return false
          }
        })
      }
    },
    handleClose() {
      this.dialogVisible = false
    },
    handMapShow() {
      this.dialogVisible = true
    },
    handleClick() {}
  }
}
</script>

<style lang="scss" scoped>
.map_btn {
  width: 82px;
  height: 22px;
  background: rgba(56, 173, 147, 1);
  border-radius: 4px;
  color: #fff;
  font-size: 14px;
  line-height: 22px;
  text-align: center;
  margin-top: 8px;
}
/deep/ .el-form-item__label {
  font-size: 14px;
  color: #000;
  font-weight: bold;
}
.form_box {
  width: 80%;
  margin: 0 auto;
  margin-top: 30px;
}
.avatar-uploader /deep/ .el-upload {
  border: 1px dashed #38ad93;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #38ad93;
  width: 150px;
  height: 150px;
  line-height: 150px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
